<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期区间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="安装路段" prop="roadSectionId">
        <el-select v-model="queryParams.roadSectionId" placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in roadSectionList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备编码" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IMEI" prop="imei">
        <el-input
          v-model="queryParams.imei"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-top"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:device:deviceAlarm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceAlarmList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50"/>
      <el-table-column label="设备编码" align="center" prop="deviceCode" width="180" />
      <el-table-column label="设备IMEI" align="center" prop="imei" width="180" />
      <el-table-column label="告警路线" align="center" prop="alarmLine" width="120" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alarm_line" :value="scope.row.alarmLine"/>
        </template>
      </el-table-column>
      <el-table-column label="告警类型" align="center" prop="alarmType" width="80" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.alarm_type" :value="scope.row.alarmType"/>
        </template>
      </el-table-column>
      <el-table-column label="告警描述" align="center" prop="bz"/>
      <el-table-column label="告警路段" align="center" prop="roadSection" width="100" />
      <el-table-column label="告警位置" align="center" prop="location" width="100" />
      <el-table-column label="告警时间" align="center" prop="alarmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.alarmTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import { listDeviceAlarm, getDeviceAlarm, addDeviceAlarm, editDeviceAlarm, delDeviceAlarm} from "@/api/business/device/deviceAlarm"
  import { getDeploymentLocationByClassLevel } from "@/api/business/device/deploymentLocation"
  export default {
    name: "DeviceAlarm",
    dicts: ['alarm_line', 'alarm_type'],
    data() {
      return {
        // 日期范围
        dateRange: [],
        disabled:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 设备列表数据
        deviceAlarmList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deviceCode: undefined,
          imei:undefined,
          roadSectionId:undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        roadSectionList:[],//路段集合
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询商品标签列表 */
      getList() {
        this.loading = true
        listDeviceAlarm(
          this.addDateRange(this.queryParams, this.dateRange)
        ).then(response => {console.log(response)
          this.deviceAlarmList = response.rows
          this.total = response.total
          this.loading = false
        })

      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          deviceCode: undefined,
          deviceName: undefined,
          imei: undefined,
          iccid: undefined,
          deviceType: undefined,
          deviceModel: undefined,
          siteId: undefined,
          site: undefined,
          roadSectionId: undefined,
          roadSection: undefined,
          locationId: undefined,
          location: undefined,
          deviceStatus: undefined,
          leader: undefined,
          installDate: undefined,
          groupId: undefined,
          groupName: undefined,
          remark: undefined,
          monitorMainline: undefined,
          monitorOneStrand: undefined,
          oneStrandVoltage: undefined,
          oneStrandRemark: undefined,
          monitorTwoStrand: undefined,
          twoStrandVoltage: undefined,
          twoStrandRemark: undefined,
          monitorThreeStrand: undefined,
          threeStrandVoltage: undefined,
          threeStrandRemark: undefined,
          monitorFourStrand: undefined,
          fourStrandVoltage: undefined,
          fourStrandRemark: undefined,
        }
        this.resetForm("form")
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = "新增"
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const deviceId = row.id || this.ids
        getDeviceAlarm(deviceId).then(response => {
          this.form = response.data
          this.open = true
          this.title = "编辑"
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              editDeviceAlarm(this.form).then(response => {
                this.$modal.msgSuccess("编辑成功")
                this.open = false
                this.getList()
              })
            } else {
              addDeviceAlarm(this.form).then(response => {
                this.$modal.msgSuccess("新增成功")
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const deviceIds = row.id || this.ids
        this.$modal.confirm('是否确认删除该标签吗？').then(function() {
          return delDeviceAlarm(deviceIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {})
      },
      /** 查询路段 */
      getRoadSection() {
        this.roadSectionList=[]
        getDeploymentLocationByClassLevel("0").then(response => {
          this.roadSectionList = response.dataconsole.log( this.roadSectionList)
        })
      },
    }
  }
</script>
